﻿using System.Linq.Expressions;
using Shop.Model.DB;
using Shop.RemoteModel.LogisticsTemplate.Model;
using Shop.Repository;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Collect.lmpl
{
    internal class LogisticsTemplateCollect : ILogisticsTemplateCollect
    {
        private readonly ILogisticsTemplateRepository _Repository;

        public LogisticsTemplateCollect (ILogisticsTemplateRepository repository)
        {
            this._Repository = repository;
        }
        public LogisticsItem[] GetItems ()
        {
            return this._Repository.Gets<LogisticsItem>(a => a.IsEnable);
        }
        public Result[] Query<Result> (LTQueryParam query, IBasicPage paging, out int count) where Result : class
        {
            return this._Repository.Query<Result>(query, paging, out count);
        }
        public long Add (LogisticsTemplateDatum datum)
        {
            if (this._Repository.IsExist(c => c.TemplateName == datum.TemplateName))
            {
                throw new ErrorException("shop.logistics.template.name.repeat");
            }
            return this._Repository.Add(datum);
        }
        public bool SetIsEnable (DBLogisticsTemplate source, bool isEnable)
        {
            if (source.IsEnable == isEnable)
            {
                return false;
            }
            this._Repository.SetIsEnable(source.Id, isEnable);
            return true;
        }
        public long SetIsDefault (DBLogisticsTemplate source)
        {
            if (source.IsDefault)
            {
                return 0;
            }
            long cancelId = this._Repository.Get(a => a.IsDefault, a => a.Id);
            this._Repository.SetIsDefault(source.Id, cancelId);
            return cancelId;
        }
        public bool Set (DBLogisticsTemplate source, LogisticsTemplateDatum datum)
        {
            if (source.TemplateName != datum.TemplateName && this._Repository.IsExist(c => c.TemplateName == datum.TemplateName))
            {
                throw new ErrorException("shop.logistics.template.name.repeat");
            }
            return this._Repository.Update(source, datum);
        }
        public Result Get<Result> (long id, Expression<Func<DBLogisticsTemplate, Result>> selector)
        {
            return this._Repository.Get<Result>(id, selector);
        }
        public Result Get<Result> (long id) where Result : class
        {
            return this._Repository.Get<Result>(id);
        }
        public DBLogisticsTemplate Get (long id)
        {
            return this._Repository.Get(id);
        }
        public void Delete (DBLogisticsTemplate source)
        {
            if (source.IsEnable)
            {
                throw new ErrorException("shop.logistics.template.already.enable");
            }
            else if (source.IsDefault)
            {
                throw new ErrorException("shop.logistics.template.not.can.delete");
            }
            this._Repository.Delete(source.Id);
        }
    }
}
